package de.baumann.browser.model;

import java.util.List;

import de.baumann.browser.api.net.util.LicenceHttpUtil;
import de.baumann.browser.api.net.util.OtherHttpUtil;
import de.baumann.browser.api.net.vo.HaseValueSet;
import de.baumann.browser.api.net.vo.HashFormat;
import de.baumann.browser.api.net.vo.LockStatus;
import de.baumann.browser.api.net.vo.LoginInfo;
import de.baumann.browser.api.net.vo.OdinOre;
import de.baumann.browser.api.net.vo.Result;
import de.baumann.browser.api.net.vo.Steal;
import de.baumann.browser.rx.RxSchedulers;
import de.baumann.browser.utils.InternalStorageUtil;
import de.baumann.browser.utils.UserDataKt;
import io.reactivex.Observable;

import static de.baumann.browser.utils.Constants.HTTP_OK;

public class MiningModel extends BaseModel {
    /**
     * 算力排名
     *
     * @return
     */
    public Observable<Result<List<LoginInfo>>> getHashRanking() {

        return OtherHttpUtil.getHashRanking()
                .compose(RxSchedulers.ioMain());
    }


    public Observable<Result<List<OdinOre>>> getOdinOres(String licence) {
        return OtherHttpUtil.getOdinOres(licence)
                .compose(RxSchedulers.ioMain());
    }


    public Observable<Result> gatherOre(OdinOre odinOre) {
        return OtherHttpUtil.gatherOre(odinOre.getOreId()).compose(RxSchedulers.ioMain());
    }

    public Observable<Result<HaseValueSet>> getHashRecord(String licence, int statrtPage) {
        return OtherHttpUtil.getHashRecord(licence, statrtPage).compose(RxSchedulers.ioMain());
    }

    public Observable<Result<List<HashFormat>>> getHashFormat() {
        return OtherHttpUtil.getHashFormat().compose(RxSchedulers.ioMain());
    }

    public Observable<Result<LoginInfo>> getLicenceStatus(String licence) {
        return LicenceHttpUtil.Companion.getLicenceStatus(licence);
    }

    public Observable<Result<List<Steal>>> getCanStealList() {
        return OtherHttpUtil.getCanStealList().compose(RxSchedulers.ioMain());
    }

    public Observable<Result<List<Steal>>> getOftenStealList() {
        return OtherHttpUtil.getOftenStealList().compose(RxSchedulers.ioMain());
    }

    public Observable<Result<LockStatus>> getGameStatus() {
        LockStatus lockStatus = (LockStatus) InternalStorageUtil.getInstance().getSerializable("gameStatus" + UserDataKt.getUserId());
        if (lockStatus != null) {
            return Observable.just(new Result<>("", HTTP_OK, lockStatus));
        }

        return OtherHttpUtil.getGameStatus()
                .map(lockStatusResult -> {
                    if (lockStatusResult.getCode() == HTTP_OK) {
                        InternalStorageUtil.getInstance().save("gameStatus" + UserDataKt.getUserId(), lockStatusResult.getData());
                    }
                    return lockStatusResult;
                })
                .compose(RxSchedulers.ioMain());
    }

    public Observable<Result<LockStatus>> setGameStatus() {
        return OtherHttpUtil.setGameStatus()
                .map(lockStatusResult -> {
                    if (lockStatusResult.getCode() == HTTP_OK) {
                        InternalStorageUtil.getInstance().save("gameStatus" + UserDataKt.getUserId(), lockStatusResult.getData());
                    }
                    return lockStatusResult;
                })
                .compose(RxSchedulers.ioMain());
    }

}
